<?php
/** 
 * EGP Framework
 *
 * LICENSE
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 */

!defined('EGP_LIB_DIR') && die('Access Deny!');

/**
 * Session 命名空间
 *
 * @package    classes
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 */
class EGP_Session_NameSpace implements IteratorAggregate
{

    /**
     * 空间名称
     *
     * @var string
     */
    protected $_namespace = 'Default';

    /**
     * 构造方法
     *
     * @param  string  $namespace
     * @throws EGP_Exception
     */
    public function __construct($namespace = 'Default')
    {
        EGP_Session::start();

        if ($namespace === '')
            throw new EGP_Exception('Session 命名空间不能为空');

        if ($namespace[0] == "_")
            throw new EGP_Exception('Session 命名空间不能以下划线开头');

        $this->_namespace = $namespace;

        if (isset($_SESSION['__EGP'][$namespace])) {
            if (isset($_SESSION['__EGP']['__EXP'][$namespace]['TIME'])) {
                if ($_SESSION['__EGP']['__EXP'][$namespace]['TIME'] < time())
                    $this->unlock()->destroy();
            }
            if (isset($_SESSION['__EGP']['__EXP'][$namespace]['HOPS'])) {
                $_SESSION['__EGP']['__EXP'][$namespace]['HOPS']--;
                if ($_SESSION['__EGP']['__EXP'][$namespace]['HOPS'] == 0)
                    $this->unlock()->destroy();
            }
        }
    }

    /**
     * 锁定，禁止修改和写入
     *
     */
    public function lock()
    {
        $_SESSION['__EGP']['__LOCK'][$this->_namespace] = true;
        return $this;
    }

    /**
     * 解除锁定
     *
     */
    public function unlock()
    {
        unset($_SESSION['__EGP']['__LOCK'][$this->_namespace]);
        return $this;
    }

    /**
     * 是否已经锁定
     *
     * @return boolean
     */
    public function isLocked()
    {
        return isset($_SESSION['__EGP']['__LOCK'][$this->_namespace]);
    }

    /**
     * 设置数据
     *
     * @param  string  $key
     * @param  mixed   $value
     * @return EGP_Session_NameSpace
     * @throws EGP_Exception
     */
    public function set($key, $value = null)
    {
        if ($this->isLocked())
            throw new EGP_Exception("Session 命名空间已经被锁定");

        if (is_array($key)) {
            foreach ($key as $k => $v) {
                $this->set($k, $v);
            }
        } else {
            $_SESSION['__EGP'][$this->_namespace][$key] = $value;
        }

        return $this;
    }

    /**
     * 魔术方法 - 设置数据
     *
     * @param  string  $key
     * @param  mixed   $value
     */
    public function __set($key, $value)
    {
        $this->set($key, $value);
    }

    /**
     * 获取数据
     *
     * @param  string  $key
     * @return mixed
     */
    public function get($key)
    {
        return $this->__isset($key) ? $_SESSION['__EGP'][$this->_namespace][$key] : null;
    }

    /**
     * 魔术方法 - 获取数据
     *
     * @param  string  $key
     * @return mixed
     */
    public function __get($key)
    {
        return $this->get($key);
    }

    /**
     * 删除数据
     *
     * @param  string  $key
     * @return EGP_Session_NameSpace
     * @throws EGP_Exception
     */
    public function del($key)
    {
        if ($this->isLocked())
            throw new EGP_Exception("Session 命名空间已经被锁定");
        unset($_SESSION['__EGP'][$this->_namespace][$key]);
        return $this;
    }

    /**
     * 魔术方法 - 删除数据
     *
     * @param  string  $key
     */
    public function __unset($key)
    {
        $this->del($key);
    }

    /**
     * 是否存在数据
     *
     * @param  string  $key
     * @return boolean
     */
    public function has($key)
    {
        return isset($_SESSION['__EGP'][$this->_namespace][$key]);
    }

    /**
     * 魔术方法 - 是否存在数据
     *
     * @param  string  $key
     * @return boolean
     */
    public function __isset($key)
    {
        return $this->has($key);
    }

    /**
     * 是否为空
     *
     * @return boolean
     */
    public function isEmpty()
    {
        return !isset($_SESSION['__EGP'][$this->_namespace]);
    }

    /**
     * 清除所有数据
     *     
     * @return EGP_Session_NameSpace
     * @throws EGP_Exception
     */
    public function clean()
    {
        if ($this->isLocked())
            throw new EGP_Exception("Session 命名空间已经被锁定");
        $_SESSION['__EGP'][$this->_namespace] = null;
        return $this;
    }

    /**
     * 销毁命名空间
     *
     * @param  boolean  $force  强制性的销毁
     * @return EGP_Session_NameSpace
     * @throws EGP_Exception
     */
    public function destroy($force = false)
    {
        if ($force == false && $this->isLocked())
            throw new EGP_Exception("Session 命名空间已经被锁定");

        unset($_SESSION['__EGP']['__LOCK'][$this->_namespace]);
        if (empty($_SESSION['__EGP']['__LOCK']))
            unset($_SESSION['__EGP']['__LOCK']);

        unset($_SESSION['__EGP']['__EXP'][$this->_namespace]);
        if (empty($_SESSION['__EGP']['__EXP']))
            unset($_SESSION['__EGP']['__EXP']);

        unset($_SESSION['__EGP'][$this->_namespace]);
        if (empty($_SESSION['__EGP']))
            unset($_SESSION['__EGP']);

        return $this;
    }

    /**
     * 获取所有数据
     *
     * @return array
     */
    public function all()
    {
        return $this->isEmpty() ? array() : $_SESSION['__EGP'][$this->_namespace];
    }

    /**
     * 取出所有数据 - 能够用于 foreach
     *
     * @return ArrayObject
     */
    public function getIterator()
    {
        return new ArrayObject($this->all());
    }

    /**
     * 设置过期时间(秒)
     *
     * @param  integer  $seconds
     * @return EGP_Session_NameSpace
     * @throws EGP_Exception
     */
    public function setExpirationSeconds($seconds)
    {
        if ($seconds <= 0)
            throw new EGP_Exception("过期时间必须大于 '0'");
        $_SESSION['__EGP']['__EXP'][$this->_namespace]['TIME'] = time() + (integer) $seconds;
        return $this;
    }

    /**
     * 设置在使用 $hops 次后过期
     *
     * @param  integer  $seconds
     * @return EGP_Session_NameSpace
     * @throws EGP_Exception
     */
    public function setExpirationHops($hops)
    {
        if ($seconds <= 0)
            throw new EGP_Exception("使用次数必须大于 '0'");
        $_SESSION['__EGP']['__EXP'][$this->_namespace]['HOPS'] = (integer) $hops;
        return $this;
    }
}
